package com.boxin.ims.modules.gift.web.front;

import java.io.File;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.boxin.framework.base.utils.Constants;
import com.boxin.ims.modules.gift.entity.Gift;
import com.boxin.ims.modules.gift.service.GiftService;
import com.boxin.ims.modules.momarketing.entity.QRCode;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.SystemService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

@Controller
@RequestMapping(value= Global.MOBILE_GIFT_PATH)
public class GiftFrontController extends BaseController {
	
	
	@Autowired
	GiftService giftService;
	@Autowired
	SystemService systemService;
	
	
	@RequestMapping(value = "/{id}" + Global.URL_SUFFIX)
	public String viewSite(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response){
		
		Gift gift = giftService.get(id);
		String pwd = request.getParameter("pwd");
		String flag = request.getParameter("flag");
		
		if(gift.getPassword() !=null && gift.getPassword().trim().length()>0  ){	//有密码
			if(!gift.getPassword().equals(pwd)){
				
				request.setAttribute("password", "1" );
				if("1".equals(flag)){
					request.setAttribute("message", "密码不正确！" );
				}
			}else{
				request.setAttribute("password", "0" );
			}
			
		}else{
			request.setAttribute("password", "0" );
		}
		File file = new File(gift.getFilePath());
		request.setAttribute("gift", gift );
		request.setAttribute("postUrl", gift.getQrCode().getContent() );
		
		request.setAttribute("fileName", file.getName() );
		
		
		//保存访问 信息
		
		//访问量
		Integer pv = gift.getPvCount();
		pv = (pv==null) ? 0: pv+1;
		
		gift.setPvCount(pv);
		giftService.save(gift);
		
		return "modules/gift/front/gift";
	}
	
	
	
	
	@RequestMapping(value = "vqr/{id}" + Global.URL_SUFFIX)
	public String viewqrcode(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response){
		
		Gift gift = giftService.get(id);
		
		QRCode qrCode = gift.getQrCode();
		
		
		File file = new File(gift.getFilePath());
		request.setAttribute("gift", gift );
		request.setAttribute("qrCode", qrCode );
		
		
		//保存访问 信息
		
		//访问量
		Integer pv = gift.getPvCount();
		pv = (pv==null) ? 0: pv+1;
		
		gift.setPvCount(pv);
		giftService.save(gift);
		
		
		
		return "modules/gift/front/giftQRCode";
	}
	
	
	
	
	@RequestMapping(value = "form")
	public String form(Gift gift, Model model) {
		
		User user = UserUtils.getUser();
		if(user == null || user.getId() == null){
			user = systemService.getUserByLoginName("yuyingchuanqing");
		}
		
		gift.setUser(user);
		
		model.addAttribute("gift", gift);
		return "modules/gift/front/giftFrontForm";
	}
	
	
	@RequestMapping(value = "save")
	public String save(@RequestParam("voiceFile") MultipartFile voiceFile,Gift gift, Model model, RedirectAttributes redirectAttributes) {
		
		if (!beanValidator(model, gift)){
			return form(gift, model);
		}
		
		
		//保存音频
		if(voiceFile != null && voiceFile.getOriginalFilename().length()>4){
			String giftVoicePath = Constants.getGiftResourceSavePath();
			String sname = voiceFile.getOriginalFilename();
			String fileType = sname.substring(sname.lastIndexOf("."));
			String fname  = "gift_"+UserUtils.getUser().getId()+"_"+System.currentTimeMillis()+fileType;
			File file = new File(giftVoicePath+fname);
			System.out.println("保存文件:"+giftVoicePath+fname);
			try {
				voiceFile.transferTo(file);
				gift.setFilePath(file.getPath());	
			} catch (Exception e) {
				System.out.println("上传文件失败:"+giftVoicePath+fname);
				e.printStackTrace();
			}
			
		}
		
		
		giftService.save(gift);
		
		addMessage(redirectAttributes, "保存礼品'" + gift.getName() + "'成功");
		return "redirect:"+Global.FRONT_PATH+"/gift/vqr/"+gift.getId()+Global.URL_SUFFIX;
	}
	
	

	

}
